package com.ruoyi.project.fujian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.project.fujian.service.IAFujianService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.project.fujian.domain.bo.AFujianBo;
import com.ruoyi.project.fujian.domain.vo.AFujianVo;
import com.ruoyi.project.fujian.domain.AFujian;
import com.ruoyi.project.fujian.mapper.AFujianMapper;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 附件Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-02
 */
@RequiredArgsConstructor
@Service
public class AFujianServiceImpl implements IAFujianService {

    @Value("${upload.path}")
    private String uploadPath;

    private final AFujianMapper baseMapper;

    /**
     * 查询附件
     */
    @Override
    public AFujianVo queryById(String fjid){
        return baseMapper.selectVoById(fjid);
    }

    /**
     * 查询附件列表
     */
    @Override
    public TableDataInfo<AFujianVo> queryPageList(AFujianBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AFujian> lqw = buildQueryWrapper(bo);
        Page<AFujianVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询附件列表
     */
    @Override
    public List<AFujianVo> queryList(AFujianBo bo) {
        LambdaQueryWrapper<AFujian> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AFujian> buildQueryWrapper(AFujianBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AFujian> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getFjname()), AFujian::getFjname, bo.getFjname());
        lqw.eq(StringUtils.isNotBlank(bo.getFjtype()), AFujian::getFjtype, bo.getFjtype());
        lqw.eq(StringUtils.isNotBlank(bo.getFjpath()), AFujian::getFjpath, bo.getFjpath());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateuserid()), AFujian::getCreateuserid, bo.getCreateuserid());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateusername()), AFujian::getCreateusername, bo.getCreateusername());
        lqw.eq(StringUtils.isNotBlank(bo.getCreatedeptid()), AFujian::getCreatedeptid, bo.getCreatedeptid());
        lqw.eq(StringUtils.isNotBlank(bo.getCreatedeptname()), AFujian::getCreatedeptname, bo.getCreatedeptname());
        lqw.eq(bo.getCreatetime() != null, AFujian::getCreatetime, bo.getCreatetime());
        return lqw;
    }

    /**
     * 新增附件
     */
    @Override
    public Boolean insertByBo(AFujianBo bo) {
        AFujian add = BeanUtil.toBean(bo, AFujian.class);
        validEntityBeforeSave(add);
        return baseMapper.insert(add) > 0;
    }

    /**
     * 修改附件
     */
    @Override
    public Boolean updateByBo(AFujianBo bo) {
        AFujian update = BeanUtil.toBean(bo, AFujian.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AFujian entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除附件
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        // 1.通过id查询数据
        for (String id : ids) {
            AFujianVo aFujianVo = queryById(id);
            Path filePath = Paths.get(uploadPath, aFujianVo.getFjpath());
            if (Files.exists(filePath)) {
                // 删除文件
                try {
                    Files.delete(filePath);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        // 如果数据库删除成功，则将附件也删除
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
